// 内核
import cloudbase from "@cloudbase/js-sdk";
// import store from '../src/store/index'
const cloudApp = cloudbase.init({
  env: "hello-cloudbase-2ghc11n5bfc69688"
});

// 匿名登录
export const login = async () => {
  const auth = cloudApp.auth();
  // 应用初始化时
  if (auth.hasLoginState()) {
    // 此时已经登录
    console.log("已经登录");
    console.log(auth);
  } else {
    const res = await auth.anonymousAuthProvider().signIn();
    // 匿名登录成功检测登录状态isAnonymous字段为true
    const loginState = auth.getLoginState();
    console.log(res);
    console.log(loginState.isAnonymousAuth); // true
    console.log("登录成功");
    console.log(res.user.uid);
    localStorage.setItem("BlogAdminUid", res.user.uid);
  }
}

// 获取首页内容，不包括主体md内容
export const ArticleList = async (pagesize, pageValue) => {
  console.log("getArticleList");
  return await cloudApp.callFunction({
    name: 'node-app',
    data: {
      switch: "getArticleList",
      PageSize: pagesize,
      pageValue: pageValue
    }
  })
}


// 获取博客详情
export const CardInfo = async (_id) => {
  console.log("getArticleList");
  return await cloudApp.callFunction({
    name: 'node-app',
    data: {
      switch: "getArticleInfo",
      id: _id
    }
  })
}

//获取总条数
export const totalNum = async () => {
  return await cloudApp.callFunction({
    name: 'node-app',
    data: {
      switch: "getTotalNum",
    }
  })
}
